library(tidyverse) # for data cleaning and plotting
library(googlesheets4) # for reading googlesheet data
library(lubridate) # for date manipulation
library(openintro) # for the abbr2state() function
library(palmerpenguins)# for Palmer penguin data
library(maps) # for map data
library(ggmap) # for mapping points on maps
library(gplots) # for col2hex() function
library(RColorBrewer) # for color palettes
library(sf) # for working with spatial data
library(leaflet) # for highly customizable mapping
library(ggthemes) # for more themes (including theme_map())
library(plotly) # for the ggplotly() - basic interactivity
library(gganimate) # for adding animation layers to ggplots
library(transformr) # for "tweening" (gganimate)
library(shiny) # for creating interactive apps
library(ggimage)
gs4_deauth() # To not have to authorize each time you knit.
theme_set(theme_minimal())
# SNCF Train data
small_trains <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/small_trains.csv")
# Lisa's garden data
garden_harvest <- read_sheet("https://docs.google.com/spreadsheets/d/1DekSazCzKqPS2jnGhKue7tLxRU3GVL1oxi-4bEM5IWw/edit?usp=sharing") %>%
mutate(date = ymd(date))
# Lisa's Mallorca cycling data
mallorca_bike_day7 <- read_csv("https://www.dropbox.com/s/zc6jan4ltmjtvy0/mallorca_bike_day7.csv?dl=1") %>%
select(1:4, speed)
# Heather Lendway's Ironman 70.3 Pan Am championships Panama data
panama_swim <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_swim_20160131.csv")
panama_bike <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_bike_20160131.csv")
panama_run <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_run_20160131.csv")
#COVID-19 data from the New York Times
covid19 <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
beyonce_lyrics <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-29/beyonce_lyrics.csv')
taylor_swift_lyrics <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-29/taylor_swift_lyrics.csv')
sales <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-29/sales.csv')
charts <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-29/charts.csv')
census_pop_est_2018 <- read_csv("https://www.dropbox.com/s/6txwv3b4ng7pepe/us_census_2018_state_pop_est.csv?dl=1") %>%
separate(state, into = c("dot","state"), extra = "merge") %>%
select(-dot) %>%
mutate(state = str_to_lower(state))
Go here or to previous homework to remind yourself how to get set up.
Once your repository is created, you should always open your project rather than just opening an .Rmd file. You can do that by either clicking on the .Rproj file in your repository folder on your computer. Or, by going to the upper right hand corner in R Studio and clicking the arrow next to where it says Project: (None). You should see your project come up in that list if you’ve used it recently. You could also go to File –> Open Project and navigate to your .Rproj file.
Put your name at the top of the document.
For ALL graphs, you should include appropriate labels.
Feel free to change the default theme, which I currently have set to theme_minimal().
Use good coding practice. Read the short sections on good code with pipes and ggplot2. This is part of your grade!
NEW!! With animated graphs, add eval=FALSE to the code chunk that creates the animation and saves it using anim_save(). Add another code chunk to reread the gif back into the file. See the tutorial for help.
When you are finished with ALL the exercises, uncomment the options at the top so your document looks nicer. Don’t do it before then, or else you might miss some important warnings and messages.
ggplotly() function.charts <- charts %>%
rename("country" = "chart")
saleChart <- sales %>%
left_join(charts, by = c("artist", "country", "released", "re_release", "label", "formats", "title")) %>%
filter(!country %in% c("World", "WW"))
saleChartPlot <- saleChart %>%
ggplot(aes(x = sales, y = reorder(title, sales), fill = country, color = artist)) +
geom_bar(stat = "identity", position = "stack") +
scale_fill_manual(values = c("yellow", "pink", "turquoise3", "green", "red", "white", "gold")) +
scale_color_manual(values = c("blue", "purple")) +
labs(title = "Sales by Album, Artist, and Country",
x = "Sales",
y = "Album",
fill = "Country",
color = "Artist",
caption = "Alex Denzler")
garden_harvest_Lettuce <- garden_harvest %>%
filter(vegetable == "lettuce") %>%
ggplot(aes(y = fct_rev(fct_infreq(variety)))) +
geom_bar(fill = "green4", color = "black") +
labs(title = "Frequency of Lettuce Harvest by Variety",
x = "Count",
y = "Variety")
ggplotly(saleChartPlot)
ggplotly(garden_harvest_Lettuce)
small_trains dataset that contains data from the SNCF (National Society of French Railways). These are Tidy Tuesday data! Read more about it here.small_trains_late <- small_trains %>%
filter(delayed_number > .15) %>%
mutate(prop_late = num_arriving_late/total_num_trips) %>%
drop_na(service)
small_trains_late %>%
ggplot(aes(x = num_arriving_late, y = factor(month), fill = service)) +
geom_violin() +
labs(title = "Distribution of Late Arrivals",
x = "Number of Late Arrivals",
y = "Month",
fill = "Service Type")
geom_area() examples here). You will look at cumulative harvest of tomato varieties over time. You should do the following:garden_harvest data, filter the data to the tomatoes and find the daily harvest in pounds for each variety.fct_reorder()) from most to least harvested (most on the bottom).garden_harvestT <- garden_harvest %>%
filter(vegetable == "tomatoes") %>%
group_by(variety, date) %>%
summarize(daily_harvest = sum(weight)*0.00220462) %>%
mutate(day_of_week = wday(date, label = TRUE)) %>%
ungroup() %>%
mutate(variety = fct_reorder(variety, daily_harvest, sum, .desc = TRUE),
cumHarv = cumsum(daily_harvest))
garden_harvestT %>%
ggplot(aes(x = date, y = cumHarv, fill = variety)) +
geom_area(position = "stack", alpha = .6) +
labs(title = "Harvest Area Plot by Variety",
x = "Date",
y = "Cumulative Harvest",
fill = "Variety") +
transition_reveal(date)
anim_save("tomato_date.gif")
mallorca_bike_day7 bike ride using animation! Requirements:ggmap.ggimage package and geom_image to add a bike image instead of a red point. You can use this image. See here for an example.mallorca <- get_stamenmap(
bbox = c(left = 2.3, bottom = 39.52, right = 2.73, top = 39.713),
maptype = "terrain",
zoom = 12)
mallorca_bike_day7 <- mallorca_bike_day7 %>%
mutate(image = "https://raw.githubusercontent.com/llendway/animation_and_interactivity/master/bike.png")
ggmap(mallorca) +
geom_point(data = mallorca_bike_day7,
aes(x = lon, y = lat), size = .5, color = "red") +
geom_path(data = mallorca_bike_day7, aes(x = lon, y = lat, color = ele), size = 1.7) +
geom_image(data = mallorca_bike_day7, aes(x = lon, y = lat, image = image), size = .1) +
geom_text(data = mallorca_bike_day7, aes(label = speed)) +
scale_color_viridis_c(option = "plasma") +
theme_map() +
theme(legend.background = element_blank()) +
labs(title = "Lisa's Bike Path in Mallorca",
subtitle = "Time: {frame_along}",
color = "Elevation") +
transition_reveal(time) +
exit_recolor(color = "black")